package com.treasure.mall.zbqmallmanager.controller.wxtemplate;

import com.treasure.mall.biz.entity.user.UserPO;
import com.treasure.mall.biz.remote.wx.WxTemplateMessageService;
import com.treasure.mall.biz.remote.wx.WxTemplateMessageType;
import com.treasure.mall.biz.remote.wx.dto.LiveTemplateDTO;
import com.treasure.mall.biz.service.core.user.UserService;
import com.treasure.mall.zbq.common.base.ApiResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 
 * <p> 微信公众号消息
 * +---------------------------------------------------------
 * * @package: com.treasure.mall.zbqmallmanager.controller.wxtemplate
 * * @author: guomw (guomwchen@foxmail.com)
 * * @date: 2020/9/1 2:26 下午
 * * @since: V1.0.0
 * +---------------------------------------------------------
 * 注意：本内容仅限于内部传阅，禁止外泄以及用于其他的商业目的
 * 
 */
@Controller
@RequestMapping("/wx/template")
public class WxTemplateController {

    @Autowired
    private WxTemplateMessageService templateMessageService;
    @Autowired
    private UserService userService;

    /**
     * 直播间模板消息
     *
     * @param model
     * @return
     */
    @RequestMapping("/live/template")
    public String getBaseConfig(Model model) {
        model.addAttribute("subscribeCount", templateMessageService.countBySubscribeIsTrue());
        return "wxtemplate/live_template";
    }

    /**
     * 发送直播间模板消息
     *
     * @param dto
     * @return
     */
    @RequestMapping("/live/template/send")
    @ResponseBody
    public ApiResult submitLiveTemplateMessage(LiveTemplateDTO dto) {
        dto.setTemplateType(WxTemplateMessageType.SERVICE_START2.getCode());
        templateMessageService.sendLiveRoomMessage(dto);
        return ApiResult.ok();
    }

    /**
     * 给指定用户发送直播间模板消息
     *
     * @param dto
     * @param testUserMobile
     * @return
     */
    @RequestMapping("/live/template/send/user")
    @ResponseBody
    public ApiResult submitLiveTemplateMessageByUser(LiveTemplateDTO dto, String testUserMobile) {
        UserPO user = userService.findByUsername(testUserMobile);
        if (user == null) {
            return ApiResult.error("用户不存在");
        }
        dto.setTemplateType(WxTemplateMessageType.SERVICE_START2.getCode());
        templateMessageService.sendUserByLiveRoomMessage(user.getUserId(), dto);
        return ApiResult.ok();
    }


}
